#include "bbDx9.h"

bbDx9::bbDx9(HWND bbWindowTarget /* = NULL */, int width /* = 100 */, int height /* = 100 */, bool fullscreen /* = NULL */, bool vsync /* = NULL */)
{
	bbWidth		 = width;
	bbHeight	 = height;
	bbFullScreen = fullscreen;
	bbVsync	     = vsync;
	bbTarget	 = bbWindowTarget;
}
bbDx9::~bbDx9()
{
	if( g_pd3dDevice != NULL )
		g_pd3dDevice->Release();

	if( g_pD3D != NULL )
		g_pD3D->Release();
}
bool bbDx9::bb_CreateDevice()
{
	if (bbFullScreen) 
	{
		return bb_CreateFullscreenDevice(bbTarget, bbWidth, bbHeight, bbVsync);
	}else{
		return bb_CreateWindowedDevice(bbTarget, bbWidth, bbHeight, bbVsync);
	}
}
/*		private		*/
bool bbDx9::bb_CreateFullscreenDevice(HWND bbWindowTarget/* = NULL*/, int width/* = 100*/, int height/* = 100*/, bool vsync/* = NULL*/)
{
	/* coming soon */
	return false;
}
bool bbDx9::bb_CreateWindowedDevice(HWND bbWindowTarget/* = NULL*/, int width/* = 100*/, int height/* = 100*/, bool vsync/* = NULL*/)
{
	g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );

	if( g_pD3D == NULL )
	{
		MessageBox(bbWindowTarget, "Direct3DCreate8 failed", "DirectX Module Error", MB_ICONERROR | MB_OK);
		return false;
	}

	D3DDISPLAYMODE d3ddm;

	if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
	{
		MessageBox(bbWindowTarget, "GetAdapterDisplayMode failed", "DirectX Module Error", MB_ICONERROR | MB_OK);
		return false;
	}

	HRESULT hr;

	if( FAILED( hr = g_pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16 ) ) )
	{
		if( hr == D3DERR_NOTAVAILABLE )
			MessageBox(bbWindowTarget, "POTENTIAL PROBLEM: We need at least a 16-bit z-buffer!", "DirectX Module Error", MB_ICONERROR | MB_OK);
			return false;
	}

	D3DCAPS9 d3dCaps;

	if( FAILED( g_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps ) ) )
	{
		MessageBox(bbWindowTarget, "GetDeviceCaps failed", "DirectX Module Error", MB_ICONERROR | MB_OK);
		return false;
	}

	DWORD dwBehaviorFlags = 0;

	if( d3dCaps.VertexProcessingCaps != 0 )
		dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
		dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	D3DPRESENT_PARAMETERS d3dpp;
	memset(&d3dpp, 0, sizeof(d3dpp));

	d3dpp.BackBufferFormat       = d3ddm.Format;
	d3dpp.SwapEffect             = (vsync)? D3DSWAPEFFECT_COPY: D3DSWAPEFFECT_DISCARD;
	d3dpp.Windowed               = TRUE;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	d3dpp.PresentationInterval   = (vsync)? D3DPRESENT_INTERVAL_ONE: D3DPRESENT_INTERVAL_IMMEDIATE;

	if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, bbWindowTarget, dwBehaviorFlags, &d3dpp, &g_pd3dDevice ) ) )
	{
		MessageBox(bbWindowTarget, "CreateDevice failed", "DirectX Module Error", MB_ICONERROR | MB_OK);
		return false;
	}
	return true;
}
/*		public		*/
void bbDx9::bb_BeginScene(float r/* = 0.0f*/, float g/* = 0.0f*/, float b/* = 0.0f*/, float a/* = 0.0f*/)
{
	g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE(r,g,b,a), 1.0f, 0 );
	g_pd3dDevice->BeginScene();
}
void bbDx9::bb_EndScene()
{
	g_pd3dDevice->EndScene();
	g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}